<template>
    <div class="container">
        <transition
        @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter"
        >
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        
        <!-- 商品轮播 -->
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swipe :lunboList="lunboList" :isfull="false"></swipe>
					</div>
				</div>
			</div>
            <!-- 商品购买信息 -->
            <div class="mui-card">
				<div class="mui-card-header">小米6X 全网通 4GB+64GB 曜石黑 移动联通电信4G手机 双卡双待 智能手机</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p class="price">
                            市场价：<del>￥3600</del>&nbsp;&nbsp;销售价：<span class="nowPrice"> ￥2199</span>
                        </p>
                        <p>
                            购买数量:
                            <numberbox @getCount="getSelectedCount" :max="stock_num"></numberbox>
                            
                        </p>
                        <p>
                            <mt-button  type="primary" size="small">立即购买</mt-button>
                            <mt-button  type="danger" size="small" @click="add">加入购物车</mt-button>
                        </p>
					</div>
				</div>
			</div>
            <!-- 商品参数 -->
            <div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：2324v</p>
                        <p>库存情况：90</p>
                        <p>上架时间：2018-12-12</p>
					</div>
				</div>
				<div class="mui-card-footer">
                    <mt-button type="primary" size="large" plain @click="goDetail(id)">图文介绍</mt-button>
                    <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
                </div>
			</div>
    </div>
</template>

<script >
import swipe from '../subcom/swipe.vue'
import numberbox from '../subcom/numberbox.vue'
export default {
    data(){
        return {
            stock_num:60,
            selectedCount:1,//保存用户选中的商品数量
            ballFlag:false,//控制小球隐藏和显示的标识符
            id:this.$route.params.id,
            lunboList:[
                {img:'https://img14.360buyimg.com/n7/jfs/t1/18275/25/880/274259/5c0dc447Ee7cf219a/4ac3ec90a15ecaf7.jpg'},
                {img:'https://img13.360buyimg.com/n7/jfs/t10675/253/1344769770/66891/92d54ca4/59df2e7fN86c99a27.jpg'}
            ],
            goodInfo:{
                price:2199
            }
        }
    },
    methods:{
        goDetail(id){
            // 点击使用编程式导航，跳转到图文介绍页面
            console.log(id);
            this.$router.push({name:'goodetail',parmas:{id}});
        },
        goComment(id){
            // 点击使用编程式导航，跳转到评论页面
            this.$router.push({name:'goodcomment',parmas:{id}});

        },
        add(){
            // 添加到购物车
            this.ballFlag=!this.ballFlag;
            // {id：商品的id,count:要购买的数量,price：商品的单价,selected:true} 
            var goodsinfo={
                id: this.id,
                count: this.selectedCount,
                price: this.goodInfo.price,
                selected: true
                }
            this.$store.commit('addTocar',goodsinfo)
            console.log(goodsinfo)
        },
        beforeEnter(el){
            el.style.transform="translate(0,0)"
        },
        enter(el,done){
            // 获取小球在页面中的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            // 徽标的距离，在组件中可以拿到dom中的所有元素
            const badgePosition = document.getElementsByClassName('mui-badge')[0].getBoundingClientRect();
            const xDist=badgePosition.x-ballPosition.x;
            const yDist = badgePosition.y-ballPosition.y;


            el.offsetWidth;
            // el.style.transform="translate("+xDist +"px,"+yDist+"px)";
            el.style.transform=`translate(${xDist}px,${yDist}px)`;
            el.style.transition="all 1s cubic-bezier(0.4,-0.3,1,0.68) ";
            done()
        },
        afterEnter(el){
            this.ballFlag=!this.ballFlag;
        },
        getSelectedCount(count){
            // 当子组件把选中的数量传递给子组件
            this.selectedCount=count;
            // 方法1：子组件向父组件传值
            console.log('子组件向父组件传值'+this.selectedCount)
            // 方法2：dom元素操作
            console.log(document.getElementById('test').value);
        }


    },
    components:{
        swipe,numberbox
    }
}
</script>


<style lang="less" scoped>
.container{
    background-color: #ccc;
    overflow: hidden;
}
.mint-swipe{
    height: 200px;
    img{
        width: 100%;
        height: 100%;
    }
}
.nowPrice{
    color: red;
    font-weight: bold;
}
.mui-card-footer{
    display: block;
    button{
        margin: 15px 0;
    }
}
.ball{
    height: 15px;
    width: 15px;
    border-radius: 50%;
    background-color: red;
    z-index: 99;
    position: absolute;
    top:408px;
    left:145px;
}
</style>

